package models

import com.jfinal.plugin.activerecord.Model

class CollectionM: Model<CollectionM>() {
    companion object{
        val AGENT by lazy {
            CollectionM()
        }
        const val TABLE_NAME = "collection"
        const val ID = "id"
        const val RECORD_ID = "record_id"
        const val USER_ID = "user_id"

        fun findByUserId(userId: Int): List<CollectionM>{
            return AGENT.find("select * from $TABLE_NAME where $USER_ID = ?", userId)
        }

        fun checkCollect(userId: Int, recordId: Int): CollectionM?{
            val list  = AGENT.find("select * from $TABLE_NAME where $USER_ID = ? and $RECORD_ID = ?", userId, recordId)
            return if (list.isNotEmpty()) list.first() else null
        }
    }
}